﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameCoder.Model;
using GameCoder.SqliteDAL;

namespace GameCoder.DAL
{
    public class GameDAL
    {
        public static GameItem Add(string name)
        {
            using (GameContent db = GameContent.Instance)
            {
                var res = from c in db.Games
                          where c.Name == name
                          select c;
                if (res.Count() > 0)
                {
                    throw new Exception("Game with the same name Exist");
                }
                Game game = new Game { ID = GetMaxID() + 1, Name = name };
                db.Games.InsertOnSubmit(game);
                db.SubmitChanges();
                return new GameItem { ID = game.ID, Name = game.Name, Groups = GetGroups(game.ID) };
            }
        }

        private static int GetMaxID()
        {
            using (GameContent db = GameContent.Instance)
            {
                var res = from c in db.Games
                          select c.ID;
                if (res.Count() == 0)
                {
                    return 0;
                }
                return res.Max();
            }
        }

        /// <summary>
        /// Delete one Game
        /// </summary>
        /// <param name="id"></param>
        public static void Delete(int id)
        {
            using (GameContent db = GameContent.Instance)
            {
                var res = from c in db.Games
                          where c.ID == id
                          select c;
                if (res.Count() == 0)
                {
                    throw new Exception("no such game");
                }
                Game game = res.Single();
                var groups = from g in db.Groups
                             where g.GameID == game.ID
                             select g;
                db.Groups.DeleteAllOnSubmit(groups);
                db.Games.DeleteOnSubmit(game);
                db.SubmitChanges();
            }
        }

        public static IList<GroupItem> GetGroups(int id)
        {
            List<GroupItem> lists = new List<GroupItem>();
            using (GameContent db = GameContent.Instance)
            {
                var res = from c in db.Games
                          where c.ID == id
                          select c;
                Console.WriteLine(db.Connection.ConnectionString);
                Console.WriteLine(db.DatabaseExists());
                if (res.Count() == 0)
                {
                    throw new Exception(string.Format("Game {0} not exist", id));
                }
                var res0 = from c in db.Groups
                           where c.GameID == id
                           select new GroupItem
                           {
                               ID = c.ID,
                               Name = c.Name
                           };
                lists = res0.ToList();
            }
            return lists;
        }

        public static GameItem GetGame(int id)
        {
            List<GroupItem> lists = new List<GroupItem>();
            using (GameContent db = GameContent.Instance)
            {
                var res = from c in db.Games
                          where c.ID == id
                          select c;
                if (res.Count() == 0)
                {
                    throw new Exception(string.Format("Game {0} not exist", id));
                }
                Game game = res.Single();

                return new GameItem { ID = game.ID, Name = game.Name, Groups = GetGroups(game.ID) };
            }
        }

        public static IList<GameItem> GetGames()
        {
            using (GameContent db = GameContent.Instance)
            {
                var res = from g in db.Games
                          select new GameItem
                          {
                              ID = g.ID,
                              Name = g.Name,
                              Groups = GetGroups(g.ID)
                          };
                return res.ToList();
            }
        }

        public static IList<GameItem> Test()
        {
            using (GameContent db = GameContent.Instance)
            {
                var res = from g in db.Games
                          where g.Groups.Count() > 0
                          select new GameItem
                          {
                              ID = g.ID,
                              Name = g.Name,
                              HasGroup=g.Groups.Count>0
                          };
                return res.ToList();
            }
        }
    }
}

